package com.wish.house.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.base.entity.*;
import com.wish.base.enums.BuildingType;
import com.wish.base.enums.CommunityType;
import com.wish.base.enums.PremisesType;
import com.wish.base.service.*;
import com.wish.house.dto.PremisesInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wish.common.log.annotation.Log;
import com.wish.common.log.enums.BusinessType;
import com.wish.common.security.annotation.PreAuthorize;
import com.wish.common.core.web.controller.BaseController;
import com.wish.common.core.web.domain.AjaxResult;
import com.wish.common.core.web.page.TableDataInfo;

/**
 * 楼盘信息Controller
 *
 * @author wish
 * @date 2022-05-25
 */
@RestController
@RequestMapping("/info")
public class TbPremisesInfoController extends BaseController {
    @Autowired
    private IPremisesInfoService tbPremisesInfoService;

    @Autowired
    private IBusinessCircleService businessCircleService;

    @Autowired
    private ISubwayService subwayService;

    @Autowired
    private ISysRegionService sysRegionService;

    @Autowired
    private IConfigPremisesPropertyService configPremisesPropertyService;

    @Autowired
    private IRelationPremisesPropertyService relationPremisesPropertyService;

    @Autowired
    private IPremisesFacilityService premisesFacilityService;

    /**
     * 查询楼盘信息列表
     */
    @PreAuthorize(hasPermi = "system:info:list")
    @GetMapping("/list")
    public TableDataInfo list(PremisesInfo tbPremisesInfo) {
        startPage();
        List<PremisesInfo> list = tbPremisesInfoService.selectTbPremisesInfoList(tbPremisesInfo);
        return getDataTable(list);
    }

    /**
     * 商圈 查询
     */
    @GetMapping("/businessList")
    public AjaxResult businessList(PremisesInfo premisesInfo) {
        QueryWrapper<BusinessCircle> wrapper = new QueryWrapper<>();
        wrapper.eq("code", premisesInfo.getCounty());
        List<BusinessCircle> businessCircles = businessCircleService.list(wrapper);
        return AjaxResult.success(businessCircles);
    }

    /**
     * 地铁查询
     */
    @GetMapping("/subwayList")
    public AjaxResult subwayList(Subway subway) {
        QueryWrapper<Subway> wrapper = new QueryWrapper<>();
        wrapper.eq("city", subway.getCity());
        List<Subway> subwayList = subwayService.list(wrapper);
        return AjaxResult.success(subwayList);
    }

    /**
     * 三级联动查询
     */
    @GetMapping("/provinceList")
    public AjaxResult provinceList(SysRegion sysRegion) {
        QueryWrapper<SysRegion> wrapper = new QueryWrapper<SysRegion>().eq("parent_id", sysRegion.getParentId());
        List<SysRegion> regionList = sysRegionService.list(wrapper);
        return AjaxResult.success(regionList);
    }

    /**
     * 获取 建筑类型
     */
    @GetMapping("/BuildingTypeList")
    public AjaxResult BuildingTypeList() {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BuildingType c : BuildingType.values()) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", c.getId());
            map.put("name", c.getName());
            list.add(map);
        }
        return AjaxResult.success(list);
    }

    /**
     * 获取 社区类型
     */
    @GetMapping("/CommunityTypeList")
    public AjaxResult CommunityTypeList() {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CommunityType c : CommunityType.values()) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", c.getId());
            map.put("name", c.getName());
            list.add(map);
        }
        return AjaxResult.success(list);
    }

    /**
     * 获取 物业类型
     */
    @GetMapping("/PremisesTypeList")
    public AjaxResult PremisesTypeList() {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PremisesType c : PremisesType.values()) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", c.getId());
            map.put("name", c.getName());
            list.add(map);
        }
        return AjaxResult.success(list);
    }

    /**
     * 查询楼盘特色
     */
    @GetMapping("/RemarkList")
    public AjaxResult RemarkList() {
        List<ConfigPremisesProperty> list = configPremisesPropertyService.list();
        return AjaxResult.success(list);
    }

    /**
     * 导出楼盘信息列表
     *//*
    @PreAuthorize(hasPermi = "system:info:export")
    @Log(title = "楼盘信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PremisesInfo tbPremisesInfo) throws IOException
    {
        List<PremisesInfo> list = tbPremisesInfoService.selectTbPremisesInfoList(tbPremisesInfo);
        ExcelUtil<PremisesInfo> util = new ExcelUtil<PremisesInfo>(PremisesInfo.class);
        util.exportExcel(response, list, "info");
    }*/

    /* *//**
     * 获取楼盘信息详细信息
     *//*
    @PreAuthorize(hasPermi = "system:info:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbPremisesInfoService.selectTbPremisesInfoById(id));
    }*/

    /**
     * 新增楼盘信息
     */
    @PreAuthorize(hasPermi = "system:info:add")
    @Log(title = "楼盘信息", businessType = BusinessType.INSERT)
    @PostMapping("/addInfo")
    public AjaxResult addInfo(@RequestBody PremisesInfoDTO premisesInfoDTO) {
        tbPremisesInfoService.save(premisesInfoDTO);
        logger.info("添加楼盘成功");

        for (Integer property:premisesInfoDTO.getPropertyList()) {
            RelationPremisesProperty relationPremisesProperty = new RelationPremisesProperty();
            relationPremisesProperty.setPropertyId(property);
            relationPremisesProperty.setPremisesId(premisesInfoDTO.getId());
            relationPremisesPropertyService.insertTbRelationPremisesProperty(relationPremisesProperty);
            logger.info("特色中间表添加成功:{}",relationPremisesProperty.toString());
        }
        for (PremisesFacility premisesFacility : premisesInfoDTO.getJiaoTongList()) {
            premisesFacility.setPremisesId(premisesInfoDTO.getId());
            premisesFacility.setType(1);
            premisesFacilityService.insertTbPremisesFacility(premisesFacility);
            System.out.println("交通添加成功:"+premisesFacility.toString());
        }
        for (PremisesFacility premisesFacility : premisesInfoDTO.getZhouBianList()) {
            premisesFacility.setPremisesId(premisesInfoDTO.getId());
            premisesFacility.setType(0);
            premisesFacilityService.insertTbPremisesFacility(premisesFacility);
            System.out.println("周边添加成功:"+premisesFacility.toString());
        }
        return AjaxResult.success();
    }

    /**
     * 修改楼盘信息
     *//*
    @PreAuthorize(hasPermi = "system:info:edit")
    @Log(title = "楼盘信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PremisesInfo tbPremisesInfo)
    {
        return toAjax(tbPremisesInfoService.updateTbPremisesInfo(tbPremisesInfo));
    }*/

    /*  *//**
     * 删除楼盘信息
     *//*
    @PreAuthorize(hasPermi = "system:info:remove")
    @Log(title = "楼盘信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbPremisesInfoService.deleteTbPremisesInfoByIds(ids));
    }*/
}
